package org.jeecg.modules.demo.ch.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.modules.demo.ch.entity.ChSparePartsManagement;
import org.jeecg.modules.demo.ch.service.IChSparePartsManagementService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.Date;

/**
 * @Description: 备品备件管理
 * @Author: jeecg-boot
 * @Date: 2022-12-16
 * @Version: V1.0
 */
@Api(tags = "备品备件管理")
@RestController
@RequestMapping("/ch/chSparePartsManagement")
@Slf4j
public class ChSparePartsManagementController extends JeecgController<ChSparePartsManagement, IChSparePartsManagementService> {
    @Autowired
    private IChSparePartsManagementService chSparePartsManagementService;

    /**
     * 分页列表查询
     *
     * @param chSparePartsManagement
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    //@AutoLog(value = "备品备件管理-分页列表查询")
    @ApiOperation(value = "备品备件管理-分页列表查询", notes = "备品备件管理-分页列表查询")
    @GetMapping(value = "/list")
    public Result<IPage<ChSparePartsManagement>> queryPageList(ChSparePartsManagement chSparePartsManagement,
                                                               @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                               @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                                               String equipmentPurchaseDate_begin,
                                                               String equipmentPurchaseDate_end,
                                                               String entryTime_begin,
                                                               String entryTime_end,
                                                               HttpServletRequest req) {
        log.info("传来的参数chSparePartsManagement:{},equipmentPurchaseDate_begin:{},equipmentPurchaseDate_end:{},entryTime_begin:{},entryTime_end:{}",chSparePartsManagement,equipmentPurchaseDate_begin,equipmentPurchaseDate_end,entryTime_begin,entryTime_end);
//        QueryWrapper<ChSparePartsManagement> queryWrapper = QueryGenerator.initQueryWrapper(chSparePartsManagement, req.getParameterMap());
        String name = chSparePartsManagement.getName();//名称
        String type = chSparePartsManagement.getType();//类型
        String state = chSparePartsManagement.getState();//状态
        String equipmentManager = chSparePartsManagement.getEquipmentManager();//设备管理人
        String equipmentFixedAssetNo = chSparePartsManagement.getEquipmentFixedAssetNo();//设备固定资产编号
        String itAssetNumber = chSparePartsManagement.getItAssetNumber();//IT资产编号
        String remarks = chSparePartsManagement.getRemarks();//备注
        String enteredBy = chSparePartsManagement.getEnteredBy();//录入人
        LambdaQueryWrapper<ChSparePartsManagement> chSparePartsManagementLambdaQueryWrapper = new QueryWrapper<ChSparePartsManagement>().lambda()
                .like(StringUtils.hasLength(name), ChSparePartsManagement::getName, name)
                .like(StringUtils.hasLength(type), ChSparePartsManagement::getType, type)
                .like(StringUtils.hasLength(state), ChSparePartsManagement::getState, state)
                .like(StringUtils.hasLength(equipmentManager), ChSparePartsManagement::getEquipmentManager, equipmentManager)
                .like(StringUtils.hasLength(equipmentFixedAssetNo), ChSparePartsManagement::getEquipmentFixedAssetNo, equipmentFixedAssetNo)
                .like(StringUtils.hasLength(itAssetNumber), ChSparePartsManagement::getItAssetNumber, itAssetNumber)
                .like(StringUtils.hasLength(remarks), ChSparePartsManagement::getRemarks, remarks)
                .like(StringUtils.hasLength(enteredBy), ChSparePartsManagement::getEnteredBy, enteredBy)
                .ge(StringUtils.hasLength(equipmentPurchaseDate_begin), ChSparePartsManagement::getEquipmentPurchaseDate, equipmentPurchaseDate_begin)
                .le(StringUtils.hasLength(equipmentPurchaseDate_end), ChSparePartsManagement::getEquipmentPurchaseDate, equipmentPurchaseDate_end)
                .ge(StringUtils.hasLength(entryTime_begin), ChSparePartsManagement::getEntryTime, entryTime_begin)
                .le(StringUtils.hasLength(entryTime_end), ChSparePartsManagement::getEntryTime, entryTime_end);
        Page<ChSparePartsManagement> page = new Page<ChSparePartsManagement>(pageNo, pageSize);
        IPage<ChSparePartsManagement> pageList = chSparePartsManagementService.page(page, chSparePartsManagementLambdaQueryWrapper);
        return Result.OK(pageList);
    }

    /**
     * 添加
     *
     * @param chSparePartsManagement
     * @return
     */
    @AutoLog(value = "备品备件管理-添加")
    @ApiOperation(value = "备品备件管理-添加", notes = "备品备件管理-添加")
    //@RequiresPermissions("org.jeecg.modules.demo:ch_spare_parts_management:add")
    @PostMapping(value = "/add")
    public Result<String> add(@RequestBody ChSparePartsManagement chSparePartsManagement) {
        //设置录入时间
        chSparePartsManagement.setEntryTime(new Date());
        chSparePartsManagementService.save(chSparePartsManagement);
        return Result.OK("添加成功！");
    }

    /**
     * 编辑
     *
     * @param chSparePartsManagement
     * @return
     */
    @AutoLog(value = "备品备件管理-编辑")
    @ApiOperation(value = "备品备件管理-编辑", notes = "备品备件管理-编辑")
    //@RequiresPermissions("org.jeecg.modules.demo:ch_spare_parts_management:edit")
    @RequestMapping(value = "/edit", method = {RequestMethod.PUT, RequestMethod.POST})
    public Result<String> edit(@RequestBody ChSparePartsManagement chSparePartsManagement) {
        chSparePartsManagementService.updateById(chSparePartsManagement);
        return Result.OK("编辑成功!");
    }

    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
    @AutoLog(value = "备品备件管理-通过id删除")
    @ApiOperation(value = "备品备件管理-通过id删除", notes = "备品备件管理-通过id删除")
    //@RequiresPermissions("org.jeecg.modules.demo:ch_spare_parts_management:delete")
    @DeleteMapping(value = "/delete")
    public Result<String> delete(@RequestParam(name = "id", required = true) String id) {
        chSparePartsManagementService.removeById(id);
        return Result.OK("删除成功!");
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @AutoLog(value = "备品备件管理-批量删除")
    @ApiOperation(value = "备品备件管理-批量删除", notes = "备品备件管理-批量删除")
    //@RequiresPermissions("org.jeecg.modules.demo:ch_spare_parts_management:deleteBatch")
    @DeleteMapping(value = "/deleteBatch")
    public Result<String> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
        this.chSparePartsManagementService.removeByIds(Arrays.asList(ids.split(",")));
        return Result.OK("批量删除成功!");
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    //@AutoLog(value = "备品备件管理-通过id查询")
    @ApiOperation(value = "备品备件管理-通过id查询", notes = "备品备件管理-通过id查询")
    @GetMapping(value = "/queryById")
    public Result<ChSparePartsManagement> queryById(@RequestParam(name = "id", required = true) String id) {
        ChSparePartsManagement chSparePartsManagement = chSparePartsManagementService.getById(id);
        if (chSparePartsManagement == null) {
            return Result.error("未找到对应数据");
        }
        return Result.OK(chSparePartsManagement);
    }

    /**
     * 导出excel
     *
     * @param request
     * @param chSparePartsManagement
     */
    //@RequiresPermissions("org.jeecg.modules.demo:ch_spare_parts_management:exportXls")
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, ChSparePartsManagement chSparePartsManagement) {
        return super.exportXls(request, chSparePartsManagement, ChSparePartsManagement.class, "备品备件管理");
    }

    /**
     * 通过excel导入数据
     *
     * @param request
     * @param response
     * @return
     */
    //@RequiresPermissions("ch_spare_parts_management:importExcel")
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, ChSparePartsManagement.class);
    }

}
